package com.hnit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class RabbitMQConfig {

    public static final String VECTORIZATION_QUEUE = "vectorization.queue";
    public static final String EXCHANGE_NAME = "vectorization.exchange";
    public static final String ROUTING_KEY = "vectorization.task";

    @Bean
    public Queue vectorizationQueue() {
        return new Queue(VECTORIZATION_QUEUE, true);
    }

    @Bean
    public TopicExchange vectorizationExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue vectorizationQueue, TopicExchange vectorizationExchange) {
        return BindingBuilder.bind(vectorizationQueue).to(vectorizationExchange).with(ROUTING_KEY);
    }

//    修改 Spring AMQP 配置，明确允许反序列化 LinkedHashMap
//    和相关类，避免设置 SPRING_AMQP_DESERIALIZATION_TRUST_ALL（因为这会禁用所有安全检查，可能不安全）。
    @Bean
    public SimpleMessageConverter messageConverter() {
        SimpleMessageConverter converter = new SimpleMessageConverter();
        converter.setAllowedListPatterns(Arrays.asList(
                "java.util.LinkedHashMap",
                "java.util.HashMap",
                "java.util.Map",
                "java.util.List",
                "java.util.ArrayList"
        ));
        return converter;
    }
}